//
//  HTTPLogger.m
//  BaoZhangWang
//
//  Created by 叶越悦 on 15/10/21.
//  Copyright © 2015年 BaoliNetworkTechnology. All rights reserved.
//

#import "HTTPLogger.h"
#import "URLResponse.h"
#import "APIManagerConfig.h"

@implementation HTTPLogger

+ (void)logWithResponer:(URLResponse *)response {
#ifdef DEBUG
    if ([APIManagerConfig shareConfig].logEnabel) {
        if (response.error) {
            [self logRequestFailure:response.request error:response.error responseObject:response.responseObject];
        } else {
            [self logRequestSuccess:response.request responseObject:response.responseObject];
        }
    }
#endif
}

+ (void)logStartRequest:(NSURLRequest *)request {
#ifdef DEBUG
    if ([APIManagerConfig shareConfig].logEnabel) {
        
        NSLog(@"==============================================");
        NSLog(@"=                Request Start               =");
        NSLog(@"==============================================");
        NSLog(@"URL:%@", request.URL);
        NSLog(@"Body:%@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
        NSLog(@"Header:%@", request.allHTTPHeaderFields ? request.allHTTPHeaderFields : @"\t\t\t\t\tN/A");
        NSLog(@"==============================================");
    }
#endif
}

+ (void)logRequestFailure:(NSURLRequest *)request error:(NSError *)error responseObject:(id)responseObject {
#ifdef DEBUG
    id obj = responseObject;
    if ([NSJSONSerialization isValidJSONObject:responseObject] && [APIManagerConfig shareConfig].logJSON) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        obj = jsonString;
    }
    
    NSLog(@"**********************************************");
    NSLog(@"*               Request Failure              *");
    NSLog(@"**********************************************");
    NSLog(@"URL:%@", request.URL);
    NSLog(@"Body:%@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
    NSLog(@"Header:%@", request.allHTTPHeaderFields ? request.allHTTPHeaderFields : @"\t\t\t\t\tN/A");
    NSLog(@"Error Info:{");
    NSLog(@" code:%@", @(error.code));
    NSLog(@" description:%@", error.description);
    NSLog(@"----------------------------------------------");
    NSLog(@"%@responseObject\n%@", obj, [APIManagerConfig shareConfig].logJSON ? @"JSON " : @"");
    NSLog(@"**********************************************");
#endif
}

+ (void)logRequestSuccess:(NSURLRequest *)request responseObject:(id)responseObject {
#if DEBUG
    if (!responseObject) {
        return;
    }
    if ([NSJSONSerialization isValidJSONObject:responseObject] && [APIManagerConfig shareConfig].logJSON) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"==============================================");
        NSLog(@"=           Request Success Service          =");
        NSLog(@"==============================================");
        NSLog(@"URL:%@", request.URL);
        NSLog(@"----------------------------------------------");
        NSLog(@"JSON responseObject\n%@", jsonString);
        NSLog(@"==============================================");
    } else {
        NSLog(@"==============================================");
        NSLog(@"=           Request Success Service          =");
        NSLog(@"==============================================");
        NSLog(@"URL:%@", request.URL);
        NSLog(@"----------------------------------------------");
        NSLog(@"responseObject\n%@", responseObject);
        NSLog(@"==============================================");
    }
#endif
}


@end
